﻿using FreeDream.Entity;
using FreeDream.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FreeDream.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BlogController : ControllerBase
    {
        private IBlogService _blogService;

        public BlogController(IBlogService blogService)
        {
            _blogService = blogService;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpPost, Route("Add")]
        public string Add(int count = 10)
        {
            try
            {
                var lstBlogAdd = new List<Blog>();
                for (int i = 0; i < count; i++)
                {
                    Blog blog = new Blog()
                    {
                        BlogId = Guid.NewGuid().ToString(),
                        Url = $"测试数据-{DateTime.Now}",
                        Title = $"测试数据的Blog,排序{DateTime.Now}",
                        Description = $"测试数据的Blog,描述：{DateTime.Now}",
                        CreateDateTime = DateTime.Now,
                    };
                    lstBlogAdd.Add(blog);
                    _blogService.Add(blog);

                    Console.WriteLine($"添加数据：{DateTime.Now}");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine($"添加数据异常：{ex}");
                return "添加数据失败";
            }
            return "添加数据成功";
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete, Route("Delete/{id}")]
        public string Delete(string id)
        {
            try
            {
                _blogService.Remove(id);
                return "删除数据成功";

            }
            catch (Exception ex)
            {
                Console.WriteLine($"删除数据异常：{ex}");
                return "删除数据失败";
            }
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Blog/{id}")]
        public string GetById(string id)
        {
            var modBlog = new Blog();
            try
            {
                modBlog = _blogService.GetById(id);
                if (modBlog != null)
                {
                    return $"{modBlog.BlogId},{modBlog.Title},{modBlog.CreateDateTime}";
                }
                else
                {
                    return "数据不存在";
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine($"程序异常：{ex}");
            }

            return "数据不存在";
        }

        [HttpGet, Route("BlogAsync")]
        public async Task<List<Blog>> GetBlogAsync(string id = "")
        {
            var lstBlog = new List<Blog>();
            var modBlog = new Blog();
            try
            {
                lstBlog = await _blogService.GetBlog(id);

            }
            catch (Exception ex)
            {
                Console.WriteLine($"程序异常：{ex}");
            }

            return lstBlog;
        }
    }
}
